#
# Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
#
# GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
# Author: Shawn.Tan
# Date: 2025.10.28
#
# History: Initial Version 2025.10.28
#
#
from PageFaultSequence import PageFaultSequence
from base.ChoicesModifier import ChoicesModifier
from gpgpu.EnvGPGPU import EnvGPGPU
from gpgpu.GenThreadGPGPU import GenThreadGPGPU
from gpgpu.ModifierUtils import PageFaultModifier


class MainSequence(PageFaultSequence):
    def __init__(self, gen_thread, name=None):
        super().__init__(gen_thread, name)

        # just a couple of load/stores...

        self._mInstrList = (
            "LD##CALM",
            "SD##CALM",
        )
        if self.getGlobalState("AppRegisterWidth") == 32:
            self._mInstrList = (
                "LW##CALM",
                "SW##CALM",
            )

        self._mExceptionCodes = (13, 15)  # for gpgpu, load: 13, store: 15
        self._mExceptionSubCodes = {}  # for now, gpgpu does not support exception sub-codes

    def createPageFaultModifier(self):
        return PageFaultModifier(self.genThread, self.getGlobalState("AppRegisterWidth"))

    def getInstructionList(self):
        return self._mInstrList

    def getExceptionCodes(self):
        return self._mExceptionCodes


def gen_thread_initialization(gen_thread):
    (delegate_opt, valid) = gen_thread.getOption("DelegateExceptions")
    if valid and delegate_opt == 1:
        # enable exception delegation for some portion of the generated tests.
        delegation_enables = ChoicesModifier(gen_thread)
        weightDict = {"0x0": 0, "0x1": 50}
        delegation_enables.modifyRegisterFieldValueChoices("medeleg.Load page fault", weightDict)
        delegation_enables.modifyRegisterFieldValueChoices(
            "medeleg.Store/AMO page fault", weightDict
        )
        delegation_enables.commitSet()


GenThreadInitialization = gen_thread_initialization

MainSequenceClass = MainSequence
GenThreadClass = GenThreadGPGPU
EnvClass = EnvGPGPU
